package com.intgrt.manager.service.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.intgrt.manager.entity.SysMenu;
import com.intgrt.manager.midentity.CombTree;
import com.intgrt.manager.service.SysMenuService;
import com.intgrt.manager.service.base.impl.BaseServiceImpl;
import com.intgrt.manager.util.ProjectTools;

@Service
public class SysMenuServiceImpl extends BaseServiceImpl implements SysMenuService {

	@Override
	public List<Map<String,Object>> getList(SysMenu entity) {
		String sql = "select s.id,s.parentid,s.menuname,s.menupath,s.menuicon,s.menushow,s.menuorder,"
				+ "s1.menuname as parentname from sys_menu s left join sys_menu s1 on s.parentid = s1.id"
				+ " where s.parentid= ";
		if(ProjectTools.isEmpty(entity.getId())){
			sql += "-1";
		}else{
			sql += entity.getId();
		}
		sql += " order by s.menuorder";
		List<Map<String,Object>> reslst = this.getMapList(sql);
		return ProjectTools.getTreeList(reslst, "menuname");
	}
	
	@Override
	public String saveEntity(SysMenu entity) {
		String sql = "insert into sys_menu (parentid,menuname,menupath,menuicon,menushow,menuorder) values ("
				+ entity.getParentid()+",'"+entity.getMenuname()+"','"+entity.getMenupath()+"','"+entity.getMenuicon()+"',"
				+ entity.getMenushow()+","+entity.getMenuorder()+")";
		return this.execute(sql)+"";
	}

	@Override
	public String editEntity(SysMenu entity) {
		String sql = "update sys_menu set parentid = "+entity.getParentid()+", menuname = '"+entity.getMenuname()+"', menupath = '"
				+ entity.getMenupath()+"',menuicon = '"+entity.getMenuicon()+"', menushow = "+entity.getMenushow()+", menuorder = "
				+ entity.getMenuorder()+" where id = "+entity.getId();
		return this.execute(sql)+"";
	}

	@Override
	public String delEntity(String ids) {
		long subnum = this.getCount("sys_menu where parentid = "+ids);
		if(subnum>0){
			return "2";
		}
		String sql = "delete from sys_menu where id = "+ids;
		return this.execute(sql)+"";
	}
	
	@Override
	public List<CombTree> getCombTree(String pid) {
		String sql = "";
		List<CombTree> comblst = new ArrayList<CombTree>();
		if(ProjectTools.isEmpty(pid)){
			sql = "select id, menuname from sys_menu where id = 1";
		}else if("1".equals(pid)){
			sql = "select id, menuname from sys_menu where parentid = "+pid;
		}else{
			return comblst;
		}
		List<Map<String, Object>> reslst =  this.getMapList(sql);
		if(reslst.size()>0){
			for(Map<String, Object> map:reslst){
				CombTree org = new CombTree(String.valueOf(map.get("id")), String.valueOf(map.get("menuname")));
				comblst.add(org);
			}
		}
		return comblst;
	}
	
	@Override
    public JSONArray getMenuArray(String id, String roleid) {
		JSONArray menuArray = new JSONArray();
		Map<String,JSONArray> sub_map = new HashMap<String,JSONArray>();
		String sql = "select menuid from sys_auth where roleid = "+roleid;
		List<String> strlst = this.getStrField(sql, "menuid");
		String menuids = "";
		if(strlst.size()>0){
			StringBuilder menubuf = new StringBuilder();
			for(String menuid: strlst){
				menubuf.append(","+menuid);
			}
			menuids = menubuf.substring(1);
		}
		sql = "select id,menuname,menuicon from sys_menu where parentid=1 "
				+ " and menushow=0 and id in ("+menuids+") order by menuorder";
		if("1".equals(id)){
			sql = "select id,menuname,menuicon from sys_menu where parentid=1 "
					+ " and menushow=0 order by menuorder";
		}
		List<Map<String,Object>> menulst = this.getMapList(sql);
		if(menulst.size()>0){
			StringBuilder pidstr = new StringBuilder();
			for(Map<String,Object> menumap:menulst){
				String menuid = String.valueOf(menumap.get("id"));
				JSONObject temp = new JSONObject();
				temp.put("id", menuid);
				temp.put("menu_name", String.valueOf(menumap.get("menuname")));
				temp.put("menu_icon", String.valueOf(menumap.get("menuicon")));
				temp.put("child_menu", new JSONObject());
				menuArray.add(temp);
				pidstr.append(","+menuid);
			}
			sql = "select id,parentid,menuname,menupath,menuicon from sys_menu where menushow=0 and "
					+ " parentid in ("+pidstr.substring(1)+") order by menuorder";
			List<Map<String,Object>> sublst = this.getMapList(sql);
			if(sublst.size()>0){
				String allids = ","+menuids+",";
				for(Map<String,Object> menumap:sublst){
					String curid = String.valueOf(menumap.get("id"));
					if(!"1".equals(id)&&allids.indexOf(","+curid+",")<0){
						continue;
					}
					String pid = String.valueOf(menumap.get("parentid"));
					JSONObject temp = new JSONObject();
					temp.put("id", curid);
					temp.put("menu_name", String.valueOf(menumap.get("menuname")));
					temp.put("menu_path", String.valueOf(menumap.get("menupath")));
					temp.put("menu_icon", String.valueOf(menumap.get("menuicon")));
					if(sub_map.containsKey(pid)){
						sub_map.get(pid).add(temp);
					}else{
						JSONArray subArray = new JSONArray();
						subArray.add(temp);
						sub_map.put(pid, subArray);
					}
				}
			}
			for(int i=0;i<menuArray.size();i++){  
				JSONObject jo = menuArray.getJSONObject(i);
				String menuid = jo.getString("id");
				if(sub_map.containsKey(menuid)){
					jo.put("child_menu", sub_map.get(menuid));
				}
			}  
		}
		return menuArray;
    }
    
}